using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class AdminDAL
    {
        public static int Save(AdminInfo info)
        {
            string sql = string.Format("insert into [admin]([UserName],[UserPwd],[RealName],[Power],[Founder],[DataQX],[CreateDate]) values(@UserName,@UserPwd,@RealName,@Power,@Founder,@DataQX,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@UserPwd", info.UserPwd),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@Power", info.Power),
                new SqlParameter("@Founder", info.Founder),
                new SqlParameter("@DataQX", info.DataQX),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(AdminInfo info)
        {
            string sql = string.Format("update [admin] set [UserName]=@UserName,[UserPwd]=@UserPwd,[RealName]=@RealName,[Power]=@Power,[Founder]=@Founder,[DataQX]=@DataQX where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@UserPwd", info.UserPwd),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@Power", info.Power),
                new SqlParameter("@Founder", info.Founder),
                new SqlParameter("@DataQX", info.DataQX),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static AdminInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[admin]");
            return Simple_Data(dr);
        }
        public static List<AdminInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[admin]");
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static AdminInfo Login(string username, string userpwd)
        {
            DataRow row = CommonDAL.GetDataRow("select * from admin where username='" + username + "' and userpwd='" + userpwd + "'");
            return Simple_Data(row);
        }
        public static List<AdminInfo> FindListByRole(int role)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [admin] where Role={0}", role));
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }

        public static List<AdminInfo> FindPage(string keyword, string model, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [admin].RealName like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(model))
            {
                filter.AppendFormat(" and [admin].UserName like '%{0}%'", model);
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[admin]", out count);
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }

        //public static List<AdminInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        //{
        //    StringBuilder filter = new StringBuilder();
        //    if (!string.IsNullOrEmpty(keyword))
        //    {
        //        filter.AppendFormat(" and [admin].Name like '%{0}%'", keyword);
        //    }
        //    if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
        //    {
        //        filter.AppendFormat(" and [admin].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
        //    }
        //    DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[admin]", out count);
        //    List<AdminInfo> list = new List<AdminInfo>();
        //    foreach (DataRow row in dt.Rows)
        //    {
        //        list.Add(Simple_Data(row));
        //    }
        //    return list;
        //}
        public static List<AdminInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [admin]"));
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        private static AdminInfo Simple_Data(DataRow dr)
        {
            AdminInfo info = new AdminInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
                info.UserPwd = DESEncrypt.Decrypt(DataConvert.ToString(dr["UserPwd"]));
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.Power = DataConvert.ToString(dr["Power"]);
                info.Founder = DataConvert.ToString(dr["Founder"]);
                info.DataQX = DataConvert.ToInt(dr["DataQX"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
    }
}

